package test.multithreading;

/**
 * Main
 *
 * @Author : Ruoyu Wang
 * @DateTime : 2024/8/21 16:11
 */


public class NThread {
    public static final Object lock = new Object();
    private static volatile int x = 0;
    private static int cnt = 10;

    public static void main(String[] args) {
        Thread[] threads = new Thread[cnt];
        for (int i = 0; i < cnt; i++) {
            final int ii = i;
            threads[i] = new Thread(() -> {
                while (x < 100) {
                    if (x % cnt == ii) {
                        synchronized (lock) {
                            System.out.println(ii + ": " + x++);
                        }
                    }
                }
            });
        }
        for (Thread thread : threads) {
            thread.start();
        }
    }
}
